#! /usr/bin/env python
#coding=utf-8
from web import form
import utils

class Submit(form.Input):

    def __init__(self, description, *validators, **attrs):
        super(Submit, self).__init__('submit', *validators, **attrs)
        self.description = ''
        self.label = description

    def render(self):
        return '<input type="submit" name="submit" value="%s" />' % self.label


valid_email = r'^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'

def comment_form(randnum):
    return form.Form(
        form.Textbox('author', form.notnull, description='Name (required)', size=22),
        form.Textbox('email', form.regexp(valid_email, "Invalid email address."),
                     description='Email (will not be published) (required)', size=22),
        form.Textbox('url', description="Website", size=22),
        form.Textbox('antispam', form.notnull, description="Type number %d here (required)" % randnum, size=22),
        form.Hidden('randnum', value=randnum),
        form.Textarea('content', form.notnull, description=None, rows='10', cols='60', id="comment"),
        Submit('Submit Comment'))

def comment_is_spam(i):
    x = utils.safeint(i.antispam)
    y = utils.safeint(i.randnum)
    return bool(x != y)

def entry_form(entry=None):
    if not entry:
        f = form.Form(
            form.Textbox('title', form.notnull, description='Title'),
            form.Textbox('tags', description='Tags (not for pages)'),
            form.Dropdown('entype', ['post', 'page'], description='Entry Type', value='post'),
            form.Textarea('content', form.notnull, description='', rows='25', cols='75', id="content"),
            Submit('Publish'))
    else:
        f = form.Form(
            form.Textbox('title', form.notnull, description='Title',
                            value=entry.title),
            form.Textbox('tags', form.notnull, description='Tags (not for pages)',
                            value=', '.join(entry.tags)),
            form.Dropdown('entype', ['post', 'page'], description='Entry Type', value=entry.entype),
            form.Textarea('content', form.notnull, description='',
                            value=entry.content, rows='25', cols='75', id="content"),
            Submit('Publish'))
    return f


def v(x):
    x = utils.safeint(x)
    return ((x >= 1) and (x <= 50))

def pref_form(config):
    return form.Form(
                form.Textbox('title', form.notnull, description='Blog Title',
                                value=config.title, size=36),
                form.Textbox('subtitle', description='Subtitle',
                                value=config.subtitle, size=36, id='blog_subtitle'),
                form.Textbox('entries_per_page', form.notnull,
                                form.regexp('\d+', 'Must be a digit'),
                                form.Validator('Range: 1~50', v),
                                description='Entry Per Page',
                                value=config.entries_per_page, size=16, maxlength=2),
                form.Dropdown('theme', utils.scan_themes(), description='Theme', value=config.theme_name),
                Submit('Save'))
